/**
 * 成员相关的验证 Schema
 */

import { Type, Static } from '@sinclair/typebox'
import { UUIDSchema, PaginationQuerySchema } from './common.schema'

// ==================== 创建成员 ====================

/**
 * 创建成员请求
 */
export const CreateMemberSchema = Type.Object(
    {
        organizationId: UUIDSchema,
        identifier: Type.String({
            minLength: 1,
            maxLength: 50,
            description: '学号/工号'
        }),
        realName: Type.String({
            minLength: 1,
            maxLength: 50,
            description: '真实姓名'
        }),
        nickName: Type.Optional(
            Type.String({
                maxLength: 50,
                description: '昵称'
            })
        ),
        wxOpenId: Type.Optional(
            Type.String({
                maxLength: 100,
                description: '微信 OpenID'
            })
        )
    },
    {
        $id: 'CreateMember',
        description: '创建成员请求'
    }
)

export type CreateMemberRequest = Static<typeof CreateMemberSchema>

/**
 * 成员信息响应
 */
export const MemberSchema = Type.Object({
    id: UUIDSchema,
    organizationId: UUIDSchema,
    identifier: Type.String(),
    realName: Type.String(),
    nickName: Type.Optional(Type.String()),
    wxOpenId: Type.Optional(Type.String()),
    createdAt: Type.String({ format: 'date-time' }),
    updatedAt: Type.String({ format: 'date-time' })
})

export const CreateMemberResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: MemberSchema
    },
    {
        $id: 'CreateMemberResponse'
    }
)

// ==================== 更新成员 ====================

/**
 * 更新成员请求
 */
export const UpdateMemberSchema = Type.Object(
    {
        realName: Type.Optional(
            Type.String({
                minLength: 1,
                maxLength: 50,
                description: '真实姓名'
            })
        ),
        nickName: Type.Optional(
            Type.String({
                maxLength: 50,
                description: '昵称'
            })
        )
    },
    {
        $id: 'UpdateMember',
        description: '更新成员请求'
    }
)

export type UpdateMemberRequest = Static<typeof UpdateMemberSchema>

export const UpdateMemberResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: MemberSchema
    },
    {
        $id: 'UpdateMemberResponse'
    }
)

// ==================== 获取成员详情 ====================

export const GetMemberResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Array(MemberSchema)
    },
    {
        $id: 'GetMemberResponse'
    }
)

// ==================== 删除成员 ====================

export const DeleteMemberResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            deleted: Type.Boolean()
        })
    },
    {
        $id: 'DeleteMemberResponse'
    }
)

// ==================== 成员列表查询 ====================

/**
 * 成员列表查询参数
 */
export const MemberListQuerySchema = Type.Intersect(
    [
        PaginationQuerySchema,
        Type.Object({
            organizationId: Type.Optional(UUIDSchema),
            displayDelete: Type.Optional(
                Type.Boolean({
                    default: false,
                    description: '是否显示已删除的成员'
                })
            )
        })
    ],
    {
        $id: 'MemberListQuery',
        description: '成员列表查询参数'
    }
)

export type MemberListQuery = Static<typeof MemberListQuerySchema>

export const MemberListResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            data: Type.Array(MemberSchema),
            total: Type.Integer(),
            offset: Type.Integer(),
            limit: Type.Integer(),
            hasMore: Type.Boolean()
        })
    },
    {
        $id: 'MemberListResponse'
    }
)

// ==================== 绑定微信 ====================

/**
 * 绑定微信请求
 */
export const BindWechatSchema = Type.Object(
    {
        memberId: UUIDSchema,
        wxOpenId: Type.String({
            minLength: 1,
            maxLength: 100,
            description: '微信 OpenID'
        }),
        nickName: Type.Optional(
            Type.String({
                maxLength: 50,
                description: '微信昵称'
            })
        )
    },
    {
        $id: 'BindWechat',
        description: '绑定微信请求'
    }
)

export type BindWechatRequest = Static<typeof BindWechatSchema>

export const BindWechatResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: MemberSchema
    },
    {
        $id: 'BindWechatResponse'
    }
)

// ==================== 批量创建成员 ====================

/**
 * 批量创建成员请求
 */
export const BatchCreateMembersSchema = Type.Object(
    {
        organizationId: UUIDSchema,
        members: Type.Array(
            Type.Object({
                identifier: Type.String({
                    minLength: 1,
                    maxLength: 50,
                    description: '学号/工号'
                }),
                realName: Type.String({
                    minLength: 1,
                    maxLength: 50,
                    description: '真实姓名'
                }),
                nickName: Type.Optional(
                    Type.String({
                        maxLength: 50,
                        description: '昵称'
                    })
                ),
                wxOpenId: Type.Optional(
                    Type.String({
                        maxLength: 100,
                        description: '微信 OpenID'
                    })
                )
            }),
            {
                minItems: 1,
                maxItems: 1000,
                description: '成员列表'
            }
        )
    },
    {
        $id: 'BatchCreateMembers',
        description: '批量创建成员请求'
    }
)

export type BatchCreateMembersRequest = Static<typeof BatchCreateMembersSchema>

export const BatchCreateMembersResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            count: Type.Integer({ description: '成功创建的数量' }),
            skipped: Type.Optional(Type.Integer({ description: '跳过的重复记录数' }))
        })
    },
    {
        $id: 'BatchCreateMembersResponse'
    }
)

// ==================== 更新成员分组 ====================

/**
 * 更新成员分组请求
 */
export const UpdateMemberGroupsSchema = Type.Object(
    {
        memberId: UUIDSchema,
        groupIds: Type.Array(UUIDSchema, {
            description: '群组ID列表'
        })
    },
    {
        $id: 'UpdateMemberGroups',
        description: '更新成员分组请求'
    }
)

export type UpdateMemberGroupsRequest = Static<typeof UpdateMemberGroupsSchema>

export const UpdateMemberGroupsResponseSchema = Type.Object(
    {
        success: Type.Literal(true),
        data: Type.Object({
            added: Type.Integer({ description: '添加的数量' }),
            removed: Type.Integer({ description: '移除的数量' }),
            failed: Type.Integer({ description: '失败的数量' })
        })
    },
    {
        $id: 'UpdateMemberGroupsResponse'
    }
)
